package com.yd.util;

import com.yd.bean.DocNodeBean;
import com.yd.bean.NodeBean;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class XmlUtil {

    public static DocNodeBean parseSourceDoc(String path) {
        SAXBuilder builder = new SAXBuilder();
        DocNodeBean docNodeBean = new DocNodeBean();
        try {
            Document document = builder.build(new File(path));
            Element resources = document.getRootElement();
            List<Element> stringList = resources.getChildren();
            for (Element element : stringList) {
                List<Attribute> attrs = element.getAttributes();
                NodeBean nodeBean = new NodeBean();
                for (Attribute attr : attrs) {
                    switch (attr.getName()) {
                        case "name":
                            nodeBean.key = attr.getValue();
                            break;
                        case "translatable":
                            nodeBean.translatable = attr.getBooleanValue();
                            break;
                    }
                }
                nodeBean.text = element.getValue();
                docNodeBean.list.add(nodeBean);
                docNodeBean.nodeMap.put(nodeBean.key, nodeBean);
            }
            return docNodeBean;
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据参考的文件排序文件键值对
     *
     * @param docNodeBean
     * @param orderDocNodeBean
     * @param outputFile
     */
    public static void orderDoc(DocNodeBean docNodeBean, DocNodeBean orderDocNodeBean, File outputFile) {
        if (docNodeBean == null || docNodeBean.list.isEmpty() || docNodeBean.nodeMap.isEmpty()) {
            System.out.println("orderDoc>>>排序参考文件 >>>为空");
            return;
        }
        if (orderDocNodeBean == null || orderDocNodeBean.list.isEmpty() || orderDocNodeBean.nodeMap.isEmpty()) {
            System.out.println("orderDoc>>>待排序参考文件 >>>为空");
            return;
        }
        if (outputFile.isFile() && outputFile.exists()) {
            outputFile.deleteOnExit();
        }
        outputFile.getParentFile().mkdirs();
        try {
            boolean result = outputFile.createNewFile();
            if (result) {
                Element resourceNode = new Element("resources");
                resourceNode.addNamespaceDeclaration(Namespace.getNamespace("tools","http://schemas.android.com/tools"));
                Document document = new Document(resourceNode);
                NodeBean temp;
                Element tempElement;
                for (NodeBean nodeBean : docNodeBean.list) {
                    temp = orderDocNodeBean.nodeMap.get(nodeBean.key);
                    if (temp != null) {
                        tempElement = new Element("string");
                        tempElement.setAttribute("name", temp.key);
                        System.out.println("orderDoc>>>添加键值对 >>>key=" + temp.key + "======text=" + temp.text);
                        tempElement.setText(temp.text);
                        resourceNode.addContent(tempElement);
                    } else {
                        System.out.println("orderDoc>>>待排序文件中不存在>>>key=" + temp.key);
                    }
                }
                XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
                xmlOutputter.output(document, new FileOutputStream(outputFile));
//        Namespace namespace = new
//        resourceNode.addNamespaceDeclaration()
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void writeMergeTranslate(DocNodeBean docNodeBean, File mergeFile) {
        mergeFile.getParentFile().mkdirs();
        try {
            if(mergeFile.exists()){
                mergeFile.delete();
            }
            boolean result = mergeFile.createNewFile();
            if (result) {
                Element resourceNode = new Element("resources");
                resourceNode.addNamespaceDeclaration(Namespace.getNamespace("tools","http://schemas.android.com/tools"));
                Document document = new Document(resourceNode);
                Element tempElement;
                for (NodeBean nodeBean : docNodeBean.list) {
                    tempElement = new Element("string");
                    tempElement.setAttribute("name", nodeBean.key);
                    System.out.println("orderDoc>>>添加键值对 >>>key=" + nodeBean.key + "======text=" + nodeBean.text);
                    tempElement.setText(nodeBean.text);
                    resourceNode.addContent(tempElement);

                }
                XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
                xmlOutputter.output(document, new FileOutputStream(mergeFile));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
